﻿using System;
using System.Data;

namespace TurtleEngine
{
	 /// <summary>
	 ///
	 /// </summary>
	 public class IndexesHelper
	 {
		  /// <summary>
		  /// The indexes
		  /// </summary>
		  private static DataTable indexes = new DataTable("Indexes");

		  /// <summary>
		  /// Gets the indexes.
		  /// </summary>
		  /// <value>
		  /// The indexes.
		  /// </value>
		  public static DataTable Indexes
		  {
				get { return indexes; }
		  }

		  /// <summary>
		  /// Gets the indexes.
		  /// </summary>
		  public static void GetIndexes()
		  {
				indexes = Utility.DBConnection.GetSchema("Indexes");
				AddColumns();

				//Utility.PrintDatatable(indexes);
		  }

		  /// <summary>
		  /// Writes the indexes.
		  /// </summary>
		  public static void WriteIndexes()
		  {
				Utility.WriteXML(indexes, indexes.TableName + ".xml");

				//Utility.PrintDatatable(indexes);
		  }

		  /// <summary>
		  /// Adds the columns.
		  /// </summary>
		  private static void AddColumns()
		  {
				indexes.Columns.Add("column_name", typeof(string));
				indexes.Columns.Add("KeyType", typeof(string));

				foreach (DataRow row in indexes.Rows)
				{
					 DataRow[] rows = (IndexColumnsHelper.IndexColumns.Select("index_name = '" + row["index_name"] + "'"));
					 string column = string.Empty, keyType = string.Empty;

					 foreach (DataRow dr in rows)
					 {
						  column += ", " + Convert.ToString(dr["column_name"]);
						  keyType += ", " + Convert.ToString(dr["KeyType"]);
					 }

					 row["column_name"] = column.Substring(1);
					 row["KeyType"] = keyType.Substring(1);
				}
		  }
	 }
}